package com.gxgy.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxgy.base.BaseAllMapper;
import com.gxgy.entity.Role;
import com.gxgy.entity.User;
import com.gxgy.service.RoleService;
import com.gxgy.uilt.IfVersionException;



/**
 * @description: 角色表 
 * @author: Qin Fengang
 * @date: 2021-11-18
 */
@Service
public class RoleServiceImpl extends BaseAllMapper implements RoleService {
	
	/**
	 * @description: 角色表 添加
	 * @author: 邓 忠柠
	 * @date: 2021-11-18
	 */

	@Transactional
	@Override
	public Integer addOneRole(String id, String roleName, String createTime) {
		if (id.isEmpty()) {
			throw new IfVersionException("角色不能为空!");
			
		}
		if (roleName.isEmpty()) {
			throw new IfVersionException("角色名称不能为空!");
		}
		if (createTime.isEmpty()) {
			throw new IfVersionException("创建时间不能为空!");
		}
		Role role = new Role();
		role.setId(id);
		role.setRoleName(roleName);
		role.setCreateTime(createTime);
		
		QueryWrapper<Role> queryId = new QueryWrapper<Role>()
				.eq(Role.ID, id);
		Role roleById = roleMapper.selectOne(queryId);
		if (roleById != null) {
			throw new IfVersionException("ID已存在");
		}
		
		QueryWrapper<Role> wrapper = new QueryWrapper<Role>()
				.eq(Role.ROLE_NAME, roleName);
		Role r = roleMapper.selectOne(wrapper);
		if (r != null) {
			throw new IfVersionException("该角色已存在！");
		}
		return roleMapper.insert(role);
	}

	/**
	 * @description: 角色 查询 通过name
	 * @author: 邓 忠柠
	 * @date: 2021-11-18
	 */
	@Override
	public Role queryRoleByName(String name) {
		if (name.isEmpty()) {
			throw new IfVersionException("角色名称不能为空");
		}
		QueryWrapper<Role> wrapper = new QueryWrapper<Role>()
				.eq(Role.ROLE_NAME, name);
		
		return roleMapper.selectOne(wrapper);
	}

	/**
	 * @description: 角色 查询 所有
	 * @author: 邓 忠柠
	 * @date: 2021-11-18
	 */
	@Override
	public List<Role> queryAllRole() {
		return  roleMapper.selectList(null);
	}
	
	

	/**
	 * @description: 角色  修改 通过id
	 * @author: 邓 忠柠
	 * @date: 2021-11-18
	 */
	@Transactional
	@Override
	public Integer updateRole(String id, String roleName, String createTime) {
		if (id.isEmpty()) {
			throw new IfVersionException("id不能为空");
		}
		if (roleName.isEmpty()) {
			throw new IfVersionException("角色名称不能为空");
		}
		if (createTime.isEmpty()) {
			throw new IfVersionException("创建时间不能为空");
		}
		Role role = new Role();
		role.setId(id);
		role.setRoleName(roleName);
		role.setCreateTime(createTime);
		QueryWrapper<Role> wrapper = new QueryWrapper<Role>()
				.eq(Role.ROLE_NAME, roleName);
		Role returnRole = roleMapper.selectOne(wrapper);
		if (returnRole !=null) {
			throw new IfVersionException("该角色已存在");
		}
		UpdateWrapper<Role> updateWrapper = new UpdateWrapper<Role>()
				.eq(Role.ID, id);
		return roleMapper.update(role, updateWrapper);
	}

	/**
	 * @description: 角色 查询 通id
	 * @author: 邓 忠柠
	 * @date: 2021-11-18
	 */
	@Override
	public Role roleById(String id) {
		if (id.isEmpty()) {
			throw new IfVersionException("ID不能为空");
		}
		QueryWrapper<Role> wrapper = new QueryWrapper<Role>()
				.eq(Role.ID, id);
		
		return roleMapper.selectOne(wrapper);
	}


	/**
	 * @description: 角色 刪除通id
	 * @author: 邓 忠柠
	 * @date: 2021-11-18
	 */
	@Override
	public Integer delectRole(String id) {
		
		return null;
	}
	
	

}
